#include <atomic>
#include<queue>

struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;

    TreeNode() : val(0), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x) : val(x), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right)
    {
    }
};

class Solution
{
public:
    std::queue<TreeNode*> _Myqueue;

    bool isCompleteTree(TreeNode* root)
    {
        bool isCom = true;
        _Myqueue.push(root);
        while (!_Myqueue.empty())
        {
            TreeNode* node = _Myqueue.front();
            if (node->left != nullptr)
            {
                _Myqueue.push(node->left);
            }
            if (node->right != nullptr)
            {
                _Myqueue.push(node->right);
            }
            if (node->right != nullptr && node->left == nullptr)
            {
                return false;
            }
            if (isCom && node->right == nullptr)
            {
                isCom = false;
                _Myqueue.pop();
                continue;
            }
            if (!isCom)
            {
                if (node->left != nullptr || node->right != nullptr)
                {
                    return false;
                }
            }
         
            _Myqueue.pop();
        }
        return true;
    }
};

